<?php
class Korisnik extends CI_Controller
{
	public function __construct()
	{
		parent::__construct();
		$this->load->model('korisnik_model','km');
		$this->load->helper(array('form','html','url'));
		session_start();
	}
	
	public function index()
	{
		$pole = array();
		$pole['kategorii'] = $this->km->prikaz_kategorii();
		$this->load->view('kategorii_view_korisnik',$pole);
	}
	
	public function motvoriK($id)
	{
		$_SESSION['kategorija'] = $id;
		$pole = array();
		$pole['podkategorii'] = $this->km->prikazPodkategorii($id);
		$this->load->view('podkategorii_view_korisnik',$pole);
	}
	
	public function motvoriPK($id)
	{
		$_SESSION['podkategorija'] = $id;
		$pole = array();
		$pole['nastani'] = $this->km->prikazNastani($id);
		$this->load->view('nastani_view_korisnik',$pole);
	}
	
	public function motvoriN($id)
	{
		$_SESSION['nastan'] = $id;
		$pole = array();
		$pole['datoteki'] = $this->km->zemiDatoteki($id);
		$this->load->view('datoteki_view_korisnik',$pole);
	}
	
	public function search()
	{
		$imenastan = $this->input->post('imenastan');
		$imedatoteka = $this->input->post('imedatoteka');
		$datumod = $this->input->post('datumod');
		$datumdo = $this->input->post('datumdo');
		
		$tip = $this->input->post('tip');
		
		if($this->input->post('search') && $this->input->post('search')=='Search' && $imenastan == "" && $imedatoteka == "" && $datumod == "" && $datumdo == "")
		{
			header("Location: ".base_url()."index.php/korisnik/search");
			exit;
		}
		
		$data = array();
		$view = 'search';
		if($this->input->post('search') && $this->input->post('search')=='Search')
		{			
			$data['tip'] = $tip;
			if($tip == 'imenastan')
			{
				$data['nastani'] = $this->km->searchNastan($imenastan);
				$view = 'nastani_view_korisnik';
			}
			else if($tip == 'imedatoteka')
			{
				$data['datoteki'] = $this->km->searchDatoteka($imedatoteka);
				$view = 'datoteki_view_korisnik';
			}
			else if($tip == 'datumnastan')
			{
				$date1 = explode("-", $datumod);
				$date2 = explode("-", $datumdo);
				if(checkdate($date1[1], $date1[2], $date1[0]) && checkdate($date2[1], $date2[2], $date2[0]))
				{
					$data['nastani'] = $this->km->searchDatum($datumod, $datumdo);
					$view = 'nastani_view_korisnik';
				}
				else
				{
					header("Location: ".base_url()."index.php/korisnik/search");
					exit;
				}
			}
		}
		$this->load->view($view,$data);
	}
}
?>